<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Permission extends Backend {

    public function __construct()
    {
        parent::__construct();
        $this->lang->load('ion_auth_acl');

        $this->data['page_home']  = lang('lang_account');
        $this->data['page_title']  = lang('lang_permission');
    }
    
    public function index()
    {
        if ( ! $this->ion_auth->logged_in() OR ! $this->ion_auth->is_admin())
        {
            redirect('auth/login', 'refresh');
        }
        else
        {
            
            // Title
			$this->data['meta_title'] = $this->data['page_title'];

			// Breadcrumbs
			$this->data['array_breadcrumbs'] = array(
				$this->data['page_home']  => 'backend',
				$this->data['page_title'] => 'backend/ionauth/users'
            );
            
            $this->data['permissions']    =   $this->ion_auth_acl->permissions('full');

            $this->data['content_header_title'] = content_header_title($this->data['page_title']);
			$this->data['content'] = 'ionauth/users/permissions';

            /* Load Template */
            $this->render();            
        }
    }
    
    public function edit($permission_id){
        if ( ! $this->ion_auth->logged_in() OR ! $this->ion_auth->is_admin())
        {
            redirect('auth/login', 'refresh');
        }
        else
       {
            // Title
			$this->data['meta_title'] = $this->data['page_title'];

			// Breadcrumbs
			$this->data['array_breadcrumbs'] = array(
				$this->data['page_home']  => 'backend',
                $this->data['page_title'] => 'backend/ionauth/permission',
                "编辑权限" => 'backend/ionauth/permission/edit'
            );

           
           if( ! $permission_id )
           {
               $this->session->set_flashdata('message', "加载出错，请重新加载");
               redirect("backend/ionauth/permission", 'refresh');
           }
           
           $permission =   $this->ion_auth_acl->permission($permission_id);
           $permission = ['perm_key'=>$permission->perm_key,'perm_name'=>$permission->perm_name];
           
           $this->form_validation->set_rules('perm_key', 'key', 'required|trim');
           $this->form_validation->set_rules('perm_name', 'name', 'required|trim');
           $this->form_validation->set_message('required', 'Please enter a %s');
           
           if( $this->form_validation->run() === FALSE )
           {
               $this->data['message']    = ($this->ion_auth_acl->errors() ? $this->ion_auth_acl->errors() : $this->session->flashdata('message'));
               $this->data['permission'] = $permission;

               $this->data['content_header_title'] = content_header_title($this->data['page_title'],"编辑权限");
               $this->data['content'] = 'ionauth/users/edit_permission';
               /* Load Template */
               $this->render(); 
           }
           else
           {
               $additional_data    =   array(
                   'perm_name' =>  $this->input->post('perm_name')
               );
           
               $update_permission = $this->ion_auth_acl->update_permission($permission_id, $this->input->post('perm_key'), $additional_data);
               if($update_permission)
               {
                   // check to see if we are creating the permission
                   // redirect them back to the admin page
                   $this->session->set_flashdata('message', $this->ion_auth->messages());
                   redirect("backend/ionauth/permission", 'refresh');
               }
           }
           
           
        }
    }
    
    public function create(){
        if ( ! $this->ion_auth->logged_in() OR ! $this->ion_auth->is_admin())
        {
            redirect('auth/login', 'refresh');
        }
        else
        {
         
 
            // Title
			$this->data['meta_title'] = $this->data['page_title'];

			// Breadcrumbs
			$this->data['array_breadcrumbs'] = array(
				$this->data['page_home']  => 'backend',
                $this->data['page_title'] => 'backend/ionauth/permission',
                "创建权限" => 'backend/ionauth/permission/create'
            );

            
            if( $this->input->post() && $this->input->post('cancel') )
                redirect('backend/ionauth/permission', 'refresh');
            
            
            if( $this->input->post()) {
                
                $this->form_validation->set_rules('perm_key', 'key', 'required|trim');
                $this->form_validation->set_rules('perm_name', 'name', 'required|trim');
                
                $this->form_validation->set_message('required', '请输入 %s');
                
                if( $this->form_validation->run() === FALSE )
                {
                    $this->data['message'] = ($this->ion_auth_acl->errors() ? $this->ion_auth_acl->errors() : $this->session->flashdata('message'));
                
                    $this->template->admin_render('backend/ionauth/users/add_permission', $this->data);
                }
                else
                {
                    $new_permission_id = $this->ion_auth_acl->create_permission($this->input->post('perm_key'), $this->input->post('perm_name'));
                    if($new_permission_id)
                    {
                        // check to see if we are creating the permission
                        // redirect them back to the admin page
                        $this->session->set_flashdata('message', $this->ion_auth->messages());
                        redirect("backend/ionauth/permission", 'refresh');
                    }
                }
            }else{

                $this->data['content_header_title'] = content_header_title($this->data['page_title'],"创建权限");
                $this->data['content'] = 'ionauth/users/add_permission';
                $this->data['message'] = $this->session->flashdata('message');
                /* Load Template */
                $this->render(); 

            }
        }
    }
}